package logic

import (
	"context"
	"doctor-medicine-system/moder/mysql"

	"doctor-medicine-system/doctorrpc/doctorrpc"
	"doctor-medicine-system/doctorrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type MedicineListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewMedicineListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *MedicineListLogic {
	return &MedicineListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *MedicineListLogic) MedicineList(in *doctorrpc.MedicineListRequest) (*doctorrpc.MedicineListResponse, error) {
	// todo: add your logic here and delete this line
	var medicine []mysql.Medicine
	mysql.DB.Scopes(mysql.Paginate(int(in.Page), int(in.Size))).Find(&medicine)
	var medicineList []*doctorrpc.MedicineList
	for _, meds := range medicine {
		list := doctorrpc.MedicineList{
			Name:     meds.Name,
			Num:      meds.Num,
			Quantity: int64(meds.Quantity),
			Price:    int64(meds.Price),
			UserId:   int64(meds.UserId),
		}
		medicineList = append(medicineList, &list)
	}
	return &doctorrpc.MedicineListResponse{
		List: medicineList,
	}, nil
}
